package com.lr.wfxmanager.merchant.controller;

import com.lr.wfxmanager.bean.GoodsListDTO;
import com.lr.wfxmanager.bean.WxbGood;
import com.lr.wfxmanager.globa.bean.JsonResultVo;
import com.lr.wfxmanager.merchant.bean.GoodsTypeBean;
import com.lr.wfxmanager.merchant.service.IGoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品管理
 */
@Controller
@RequestMapping("goods")
public class GoodsController {
    @Resource
    private RestTemplate restTemplate;
    //商品类型
    @Resource
    private IGoodsService iGoodsService;
    @ResponseBody
    @RequestMapping("type")
    public List<GoodsTypeBean> type(Model model){
        List<GoodsTypeBean> goodsType = iGoodsService.queryGoodsType();
        return goodsType;
    }
    //商品列表
    @RequestMapping("list")
    public String list(Model model,Integer currentPage){
        JsonResultVo resultVo = new JsonResultVo();
        if (null==currentPage||0==currentPage){
            currentPage = 1;
        }

        GoodsListDTO goodsListDTO = restTemplate.getForObject("http://goods-server/goods/list?currentPage=" + currentPage, GoodsListDTO.class);
        List<WxbGood> goodList = goodsListDTO.getWxbGoods();
        int size = goodList.get(0).getSize();//总商品数
        model.addAttribute("size",size);
        model.addAttribute("goodList",goodList);
        model.addAttribute("currentPage",currentPage);
        return "goods";
    }
    //上下架操作
    @ResponseBody
    @RequestMapping("line")
    public JsonResultVo line(String goodIds,Integer flag){
        JsonResultVo jsonResultVo = new JsonResultVo();
        String[] strings = goodIds.split(",");
        try {
            iGoodsService.updateGoodsState(flag,strings);
            jsonResultVo.setMark(1);
        } catch (Exception e) {
            jsonResultVo.setMark(0);
            e.printStackTrace();
        }
        return jsonResultVo;
    }
}
